/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pacman;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Rectangle2D;
import javax.swing.JPanel;

/**
 *
 * @author KimDinhSon
 */
class DrawGame extends JPanel {
    private static final long serialVersionUID = 1L;
    private final Pacman outer;

    DrawGame(final Pacman outer) {
        this.outer = outer;
    }

    @Override
    public void paintComponent(Graphics comp) {
        Graphics2D comp2d = (Graphics2D) comp;
        int x = 0;
        int y = 0;
        int tmpi = 0;
        char cByte;
        int i = 0;
        int j = 0;
        Rectangle2D.Float r2d = new Rectangle2D.Float();
        Ellipse2D.Float e2d = new Ellipse2D.Float();
        comp2d.drawImage(outer.backroundImg, 0, 0, Pacman.Width - 10, Pacman.Height - 30, this); //ve nen game
        comp2d.setColor(Color.black);
        r2d.setFrame(24, 24 * 4, 24 * 19, 24 * 22);
        comp2d.fill(r2d);
        comp2d.draw(r2d);
        for (y = 0; y < 22; y++) {
            for (x = 0; x < 19; x++) {
                cByte = Pacman.level[x][y];
                if (cByte == 'V' && (Pacman.anNgoc == true)) {
                    comp2d.drawImage(outer.VAT_PHAM, 24 + 24 * x, 96 + 24 * y, 24, 24, this);
                } else if (cByte == '.') {
                    //Vẽ vật phẩm màu đỏ trên đường đi của pacman
                    comp2d.setColor(Color.red);
                    e2d.setFrame(24 + x * 24 + 12 - 3 / 2, 24 * 4 + y * 24 + 12 - 3 / 2, 3, 3);
                    comp2d.fill(e2d);
                    comp2d.draw(e2d);
                } else if (cByte == 'P') {
                    Pacman.TOA_DO_HIEN_TAI_Px = x * 24;
                    Pacman.TOA_DO_HIEN_TAI_Py = y * 24;
                    Pacman.TOA_DO_BAN_DAU_Px = x * 24;
                    Pacman.TOA_DO_BAN_DAU_Py = y * 24;
                    Pacman.level[x][y] = '-';
                } else if (cByte == '&') {
                    Pacman.DoorX = x;
                    Pacman.DoorY = y;
                } else if (cByte == 'F') {
                    Pacman.tmpGhostSpawn = new int[Pacman.GhostSpawnMax + 1][2];
                    for (tmpi = 0; tmpi < Pacman.GhostSpawnMax; tmpi++) {
                        Pacman.tmpGhostSpawn[tmpi][0] = Pacman.GhostSpawn[tmpi][0];
                        Pacman.tmpGhostSpawn[tmpi][1] = Pacman.GhostSpawn[tmpi][1];
                    }
                    Pacman.tmpGhostSpawn[Pacman.GhostSpawnMax][0] = x * 24;
                    Pacman.tmpGhostSpawn[Pacman.GhostSpawnMax][1] = y * 24;
                    Pacman.GhostSpawn = new int[Pacman.GhostSpawnMax + 1][2];
                    Pacman.GhostSpawnMax += 1;
                    for (tmpi = 0; tmpi < Pacman.GhostSpawnMax; tmpi++) {
                        Pacman.GhostSpawn[tmpi][0] = Pacman.tmpGhostSpawn[tmpi][0];
                        Pacman.GhostSpawn[tmpi][1] = Pacman.tmpGhostSpawn[tmpi][1];
                    }
                    Pacman.level[x][y] = ' ';
                } else if (cByte == 'O') {
                    comp2d.setColor(Color.white);
                    e2d.setFrame(24 + x * 24 + 9, 24 * 4 + y * 24 + 9, 6, 6);
                    comp2d.fill(e2d);
                    comp2d.draw(e2d);
                } else if ((cByte == '@')) {
                    comp2d.drawImage(outer.DoorImg, 24 + x * 24, 24 * 4 + y * 24, 24, 24, this);
                } else {
                    if (cByte == '#') {
//                        if (Pacman.anNgoc == true) {
//                            comp2d.drawImage(outer.scaredWallImg, 24 + (x * 24), 96 + (y * 24), 24, 24, this);
//                        } else {
//                            comp2d.drawImage(outer.wallImgColum, 24 + (x * 24), 96 + (y * 24), 24, 24, this);
//                        }
                        comp2d.drawImage(outer.wallImgColum, 24 + (x * 24), 96 + (y * 24), 24, 24, this);
                    } 
                    else if (cByte == '$') {
//                        if (Pacman.anNgoc == true) {
//                            comp2d.drawImage(outer.scaredWallImg, 24 + (x * 24), 96 + (y * 24), 24, 24, this);
//                        } else {
//                            comp2d.drawImage(outer.wallImgRow, 24 + (x * 24), 96 + (y * 24), 24, 24, this);
//                        }
                        comp2d.drawImage(outer.wallImgRow, 24 + (x * 24), 96 + (y * 24), 24, 24, this);
                    }
                    else if (cByte == 'C') {
//                        if (Pacman.anNgoc == true) {
//                            comp2d.drawImage(outer.scaredWallImg, 24 + (x * 24), 96 + (y * 24), 24, 24, this);
//                        } else {
//                            comp2d.drawImage(outer.wallImgAngleC, 24 + (x * 24), 96 + (y * 24), 24, 24, this);
//                        }
                        comp2d.drawImage(outer.wallImgAngleC, 24 + (x * 24), 96 + (y * 24), 24, 24, this);
                    } 
                    else if (cByte == 'A') {
//                        if (Pacman.anNgoc == true) {
//                            comp2d.drawImage(outer.scaredWallImg, 24 + (x * 24), 96 + (y * 24), 24, 24, this);
//                        } else {
//                            comp2d.drawImage(outer.wallImgAngleA, 24 + (x * 24), 96 + (y * 24), 24, 24, this);
//                        }
                        comp2d.drawImage(outer.wallImgAngleA, 24 + (x * 24), 96 + (y * 24), 24, 24, this);
                    } 
                    else if (cByte == 'B') {
//                        if (Pacman.anNgoc == true) {
//                            comp2d.drawImage(outer.scaredWallImg, 24 + (x * 24), 96 + (y * 24), 24, 24, this);
//                        } else {
//                            comp2d.drawImage(outer.wallImgAngleB, 24 + (x * 24), 96 + (y * 24), 24, 24, this);
//                        }
                        comp2d.drawImage(outer.wallImgAngleB, 24 + (x * 24), 96 + (y * 24), 24, 24, this);
                    } else if (cByte == 'D') {
//                        if (Pacman.anNgoc == true) {
//                            comp2d.drawImage(outer.scaredWallImg, 24 + (x * 24), 96 + (y * 24), 24, 24, this);
//                        } else {
//                            comp2d.drawImage(outer.wallImgAngleD, 24 + (x * 24), 96 + (y * 24), 24, 24, this);
//                        }
                        comp2d.drawImage(outer.wallImgAngleD, 24 + (x * 24), 96 + (y * 24), 24, 24, this);
                    }
                    else if (cByte == 'R') {
//                    if (Pacman.anNgoc == true) {
//                            comp2d.drawImage(outer.scaredWallImg, 24 + (x * 24), 96 + (y * 24), 24, 24, this);
//                        } else {
//                            comp2d.drawImage(outer.wallImgAngleR, 24 + (x * 24), 96 + (y * 24), 24, 24, this);
//                        }
                    comp2d.drawImage(outer.wallImgAngleR, 24 + (x * 24), 96 + (y * 24), 24, 24, this);
                    } 
                    else if (cByte == '1') {
//                    if (Pacman.anNgoc == true) {
//                            comp2d.drawImage(outer.scaredWallImg, 24 + (x * 24), 96 + (y * 24), 24, 24, this);
//                        } else {
//                            comp2d.drawImage(outer.wallImgAngleRA, 24 + (x * 24), 96 + (y * 24), 24, 24, this);
//                    }
                    comp2d.drawImage(outer.wallImgAngleRA, 24 + (x * 24), 96 + (y * 24), 24, 24, this);
                    } 
                    else if (cByte == '2') {
//                        if (Pacman.anNgoc == true) {
//                            comp2d.drawImage(outer.scaredWallImg, 24 + (x * 24), 96 + (y * 24), 24, 24, this);
//                        } else {
//                            comp2d.drawImage(outer.wallImgAngleRB, 24 + (x * 24), 96 + (y * 24), 24, 24, this);
//                        }
                        comp2d.drawImage(outer.wallImgAngleRB, 24 + (x * 24), 96 + (y * 24), 24, 24, this);
                    } 
                    else if (cByte == '3') {
//                        if (Pacman.anNgoc == true) {
//                            comp2d.drawImage(outer.scaredWallImg, 24 + (x * 24), 96 + (y * 24), 24, 24, this);
//                        } else {
//                            comp2d.drawImage(outer.wallImgAngleRC, 24 + (x * 24), 96 + (y * 24), 24, 24, this);
//                        }
                        comp2d.drawImage(outer.wallImgAngleRC, 24 + (x * 24), 96 + (y * 24), 24, 24, this);
                        
                    } 
                    else if (cByte == '4') {
//                        if (Pacman.anNgoc == true) {
//                            comp2d.drawImage(outer.scaredWallImg, 24 + (x * 24), 96 + (y * 24), 24, 24, this);
//                        } else {
//                            comp2d.drawImage(outer.wallImgAngleRD, 24 + (x * 24), 96 + (y * 24), 24, 24, this);
//                        }
                        comp2d.drawImage(outer.wallImgAngleRD, 24 + (x * 24), 96 + (y * 24), 24, 24, this);
                    } else if (cByte == '@') {
//                        if (Pacman.anNgoc == true) {
//                            comp2d.drawImage(outer.scaredWallImg, 24 + (x * 24), 96 + (y * 24), 24, 24, this);
//                        } else {
//                            comp2d.drawImage(outer.wallImgAngleRD, 24 + (x * 24), 96 + (y * 24), 24, 24, this);
//                        }
                        comp2d.drawImage(outer.DoorImg, 24 + (x * 24), 96 + (y * 24), 24, 24, this);
                    }
                }
            }
        }
        if (Pacman.FirstLoad != 2) {
            Pacman.setupGhosts();
            for (i = 0; i < 4; i++) {
                for (j = 0; j <= 2; j++) {
                    comp2d.drawImage(outer.PAC_IMG[i][j], 24 + Pacman.TOA_DO_HIEN_TAI_Px, 96 + Pacman.TOA_DO_HIEN_TAI_Py, 24, 24, this);
                }
            }
            comp2d.drawImage(outer.NEW_GAME_IMG[0], 340, 637, 140, 35, this);
            comp2d.drawImage(outer.NEW_GAME_IMG[1], 340, 637, 140, 35, this);
            comp2d.drawImage(outer.NEW_GAME_IMG[2], 340, 637, 140, 35, this);
            Pacman.FirstLoad += 1;
        }
        //Draw player lives score and high score
        if (Pacman.Plives > 0) {
            comp2d.drawImage(outer.PAC_IMG[0][2], 75, 647, 18, 18, this);
        }
        if (Pacman.Plives > 1) {
            comp2d.drawImage(outer.PAC_IMG[0][2], 99, 647, 18, 18, this);
        }
        if (Pacman.Plives > 2) {
            comp2d.drawImage(outer.PAC_IMG[0][2], 99, 647, 18, 18, this);
        }
        //ve diem so va diem cao
        Diemso.onDraw(comp2d, Pacman.Score, Pacman.HighScore);
        //Draw GHOSTs
        for (i = 0; i < 4; i++) {
            Pacman.GhostAnimCnt[i] += 1;
            if (Pacman.GhostAnimCnt[i] >= 2) {
                Pacman.GhostAnimCnt[i] = 0;
                Pacman.GhostAnimFrame[i] += 1;
                if (Pacman.GhostAnimFrame[i] >= 2) {
                    Pacman.GhostAnimFrame[i] = 0;
                }
            }
            if (Pacman.anNgoc == true) {
                if (Pacman.GhostRecoveryChange == 0) {
                    comp2d.drawImage(outer.SCARED_GHOST[Pacman.GhostAnimFrame[i]], 24 + Pacman.GhostX[i], 96 + Pacman.GhostY[i], 24, 24, this);
                } else {
                    comp2d.drawImage(outer.SCARED_GHOST_RECOVERY[Pacman.GhostAnimFrame[i]], 24 + Pacman.GhostX[i], 96 + Pacman.GhostY[i], 24, 24, this);
                }
            } else {
                comp2d.drawImage(outer.GHOST_IMG[i][Pacman.GhostAnimFrame[i]][Pacman.GhostDir[i]], 24 + Pacman.GhostX[i], 96 + Pacman.GhostY[i], 24, 24, this);
            }
        }
        //Draw PACMAN
        comp2d.drawImage(outer.PAC_IMG[Pacman.Pdir][Pacman.PacAnimInd], 24 + Pacman.TOA_DO_HIEN_TAI_Px, 96 + Pacman.TOA_DO_HIEN_TAI_Py, 24, 24, this);
        if ("startup".equals(Pacman.GameStatus)) {
            comp2d.drawImage(outer.NEW_GAME_IMG[Pacman.NEW_GAME_IND], 340, 637, 140, 35, this);
        }
        if ("gameover".equals(Pacman.GameStatus)) {
            //game over
            comp2d.drawImage(outer.NEW_GAME_IMG[Pacman.NEW_GAME_IND], 340, 637, 140, 35, this);
            comp2d.drawImage(outer.GameOverImg, 108, 240, 300, 200, this);
            Pacman.DIEM_LEVEL = 0;
            Pacman.Score= 0;
            Pacman.HighScore = Diemso.LoadHighScore();
            Pacman.LoadMap();
        }
        if ("paused".equals(Pacman.GameStatus)) {
            comp2d.drawImage(outer.PausedImg, 108, 240, 300, 200, this);
        }
        if ("lost".equals(Pacman.GameStatus)) {
            comp2d.drawImage(outer.LostImg, 108, 240, 300, 160, this);
        }
        if ("won".equals(Pacman.GameStatus)) {
            //Chien Thang
            comp2d.drawImage(outer.NEW_GAME_IMG[Pacman.NEW_GAME_IND], 340, 637, 140, 35, this);
            comp2d.drawImage(outer.WonImg, 108, 240, 300, 160, this);
            Pacman.DIEM_LEVEL++;
            Pacman.LoadMap();
        }
    }
    
}
